package com.tianqu.tms.www.proxymember.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.CarStyle;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.www.proxymember.actionform.ProxymemberCreationInputActionForm;
import com.tianqu.tms.www.proxymember.viewhelper.ProxyMemberCarSearchInputViewHelper;

/**
 * @author zhangxueliang
 * 待办车辆修改确认页面action
 */
public class ProxyMemberCarUpdateConfirmAction extends TmsAction{
	private Logger logger = Logger
	.getLogger(ProxyMemberCarUpdateConfirmAction.class);
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		ProxyMemberCarSearchInputViewHelper viewhelper = new ProxyMemberCarSearchInputViewHelper();
		ProxymemberCreationInputActionForm proxyForm = (ProxymemberCreationInputActionForm)form;
		viewhelper = (ProxyMemberCarSearchInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
		viewhelper = formToViewHelper(viewhelper,proxyForm);

		uc.setAttribute(TmsConstants.VIEWHELP, viewhelper);
		request.setAttribute(VIEWHELP, viewhelper);
		
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}
	/**
	 * form ->viewHelper
	 * @param viewHelper
	 * @param proxyForm
	 * @return
	 * @throws TmsDaoException 
	 */
	private ProxyMemberCarSearchInputViewHelper formToViewHelper(ProxyMemberCarSearchInputViewHelper viewHelper,
			ProxymemberCreationInputActionForm proxyForm) throws TmsDaoException{
		
		//
		Codes code = Codes.getInstance();
		CarStyle carStyle = CarStyle.getInstance();
		viewHelper.setCardType(code.getItemValueByItemIdAndItemCode(Codes.PLATE_TYPE, proxyForm.getCardType()));
		
		viewHelper.setCarType(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_TYPE,proxyForm.getCarType()));
		viewHelper.setCarUse(code.getItemValueByItemIdAndItemCode(Codes.USER_NATURE,proxyForm.getCarUse()));
		viewHelper.setColor(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COLOR,proxyForm.getColor()));
		viewHelper.setCountryStyle(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COUNTRY,proxyForm.getCountryStyle()));
		
		viewHelper.setGearbox(code.getItemValueByItemIdAndItemCode(Codes.GEARBOX_TYPE,carStyle.getGearboxByCarStyleId(proxyForm.getCarStyleId())));
		viewHelper.setFuelType(code.getItemValueByItemIdAndItemCode(Codes.FUEL_TYPE,proxyForm.getFuelType()));
		viewHelper.setOwnerType(code.getItemValueByItemIdAndItemCode(Codes.CAR_OWNER,proxyForm.getOwnerType()));
		viewHelper.setGearbox(code.getItemValueByItemIdAndItemCode(Codes.GEARBOX_TYPE,proxyForm.getGearbox()));
		viewHelper.setCarName(carStyle.getCarStyleRealNameById(proxyForm.getCarStyleId()));
		return viewHelper;
	}
}
